require(animation)
## read in datafile
s04 <- read.csv('s04-coin-flips.csv')
## turn into unstructured vector
s04.vec <- unlist(s04)

## need 6 boxes
.breaks=(0:6)-1e-6

ani.options(interval = 0.1, ani.width=600, ani.height=400)
## requires local installation of ffmpeg
saveVideo({
    layout(1:2)
    ## shrink margins
    par(mar=c(4,4,1,1))
    ## plot a histogram by freq and density,
    ## add coinflips one by one
    for( ii in 1:length(s04.vec)) {
        hist(s04.vec[1:ii], breaks=.breaks, freq=T, xlab='', main='')
        ## add counter at bottom
        hist(s04.vec[1:ii], breaks=.breaks, freq=F, 
            main='', ylim=c(0,0.5), xlab=sprintf('Number of heads, %s flips', ii)
        )
    }},
    ## high res
    video.name = "coinflips-nheads.avi", 
    other.opts = "-b 2500k"  # higher bitrate,  better quality
)
